﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;

/// <summary>
/// Summary description for ProcessIO
/// </summary>
public class ProcessIO
{
    public ProcessIO()
    {
        //
        // TODO: Add constructor logic here
        //
    }

    public static bool ExportExcel(Page page, System.Web.UI.WebControls.GridView grv, string filename)
    {
        HttpResponse response = page.Response;
        System.Web.UI.HtmlControls.HtmlForm form = new System.Web.UI.HtmlControls.HtmlForm();

        string attachment = "attachment; filename=" + filename;
        response.ClearContent();
        response.AddHeader("content-disposition", attachment);
        response.ContentType = "application/ms-excel";
        response.Charset = "";
        response.ContentEncoding = System.Text.Encoding.Unicode;
        response.BinaryWrite(System.Text.Encoding.Unicode.GetPreamble());

        System.IO.StringWriter stw = new System.IO.StringWriter();
        HtmlTextWriter htextw = new HtmlTextWriter(stw);
        form.Controls.Add(grv); // gvParent là gridview 
        page.Controls.Add(form);
        form.RenderControl(htextw);

        response.Write(stw.ToString());
        response.End();

        return true;
    }
}
